<?php
   
/**
 * Mapper for tcomments
 * 
 * @author Martin Grossmann <snake.aas@gmail.com>
 * @version 1.0
 * @package Mappers
 *
 * @property int $idComment ID
 * @property User $User User that entered the comment
 * @property Photo $Photo Photo, which belongs to comment
 * @property-read DateTime $InsertionDate Date added comment
 * @property string $Text Text of a comment. 65535 chars max
 * 
 * @entity
 * @table(name="tcomments", indexes={@index(name="TComments_TPhotos_FK", columns="idPhoto")})
 * @HasLifecycleCallbacks
 */     
class Comment {
  /**
   *
   * @access private
   * @var int 
   * 
   * @Id
   * @column(name="idComment", type="integer")
   * @generatedValue
   */
  private $idComment;

  /**
   * User that entered the comment
   * 
   * @access private
   * @var User
   * 
   * @manyToOne(targetEntity="User", inversedBy="comments")
   * @joinColumn(name="idUser", referencedColumnName="idUser")
   */
   private $user;
     
  /**
   * Photo, which belongs to comment
   * 
   * @access private
   * @var Photo
   * 
   * @manyToOne(targetEntity="Photo", inversedBy="comments")
   * @joinColumn(name="idPhoto", referencedColumnName="idPhoto")
   */
   private $photo;

  /**
   * Date added comment
   * 
   * @access private
   * @var DateTime
   * 
   * @column(name="InsertionDate", type="datetimetz")
   */
   private $insertionDate;
     
  /**
   * Text of a comment
   * 
   * @access private
   * @var string
   * 
   * @column(name="Text", type="text")
   */
   private $text;
     

  public function __get($name) {
  
    switch($name) {
      case "idComment":
        return $this->idComment;
        break;
      
      case "User": 
        return $this->user;
        break;
   
      case "Photo": 
        return $this->photo;
        break;
    
      case "InsertionDate": 
        return $this->insertionDate;
        break;
    
      case "Text": 
        return $this->text;
        break;
      
      default: 
        throw new EntityException(sprintf(T_("Trying to get undefined property")." ", $name));
    }
  }
  
  public function __set($name, $value) {
        
    switch($name) {            
      case "User": 
        $this->user = Validations::ValidateObject($value, 'User');
        break; 

      case "Photo": 
        $this->photo = Validations::ValidateObject($value, 'Photo');
        break; 

      case "Text": 
        $this->text = Validations::validateString($value, 65535, null, false);
        break; 

      default: 
        throw new EntityException(sprintf(T_("Trying to set undefined property")." ", $name));
    }
  }  
  
  public function __construct() {
    
  }
  
  /**
   * @prePersist
   */
  public function preInsert() {
    $this->insertionDate = new DateTime('now');
  }
  
}